﻿namespace System.Text
{
	/// <summary>
	/// Represents a CP866U character encoding of Unicode characters.
	/// </summary>
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1722:IdentifiersShouldNotHaveIncorrectPrefix")]
	public sealed class CP866UEncoding : Encoding
	{
		private readonly Encoder _encoder = new CP866UEncoder();
		private readonly Decoder _decoder = new CP866UDecoder();
		
		/// <summary>
		/// Obtains an encoder that converts a sequence of Unicode characters into a CP866U encoded sequence of bytes.
		/// </summary>
		/// <returns>An <see cref="T:System.Text.Encoder" /> that converts a sequence of Unicode characters into a CP866U encoded sequence of bytes.</returns>
		public override Encoder GetEncoder()
		{
			return new CP866UEncoder();
		}

		/// <summary>
		/// Obtains a decoder that converts a CP866U encoded sequence of bytes into a sequence of Unicode characters.
		/// </summary>
		/// <returns>A <see cref="T:System.Text.Decoder" /> that converts a CP866U encoded sequence of bytes into a sequence of Unicode characters.</returns>
		public override Decoder GetDecoder()
		{
			return new CP866UDecoder();
		}

		/// <summary>
		/// Calculates the number of bytes produced by encoding a set of characters from the specified character array.
		/// </summary>
		/// <param name="chars">The character array containing the set of characters to encode.</param>
		/// <param name="index">The index of the first character to encode.</param>
		/// <param name="count">The number of characters to encode.</param>
		/// <returns>The number of bytes produced by encoding the specified characters.</returns>
		public override int GetByteCount(char[] chars, int index, int count)
		{
			return _encoder.GetByteCount(chars, index, count, false);
		}

		/// <summary>
		/// Encodes a set of characters from the specified character array into the specified byte array.
		/// </summary>
		/// <param name="chars">The character array containing the set of characters to encode.</param>
		/// <param name="charIndex">The index of the first character to encode.</param>
		/// <param name="charCount">The number of characters to encode.</param>
		/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
		/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
		/// <returns>The actual number of bytes written into bytes.</returns>
		public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
		{
			return _encoder.GetBytes(chars, charIndex, charCount, bytes, byteIndex, false);
		}

		/// <summary>
		/// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.
		/// </summary>
		/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
		/// <param name="index">The index of the first byte to decode.</param>
		/// <param name="count">The number of bytes to decode.</param>
		/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
		public override int GetCharCount(byte[] bytes, int index, int count)
		{
			return _decoder.GetCharCount(bytes, index, count);
		}

		/// <summary>
		/// Decodes a sequence of bytes from the specified byte array into the specified character array.
		/// </summary>
		/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
		/// <param name="byteIndex">The index of the first byte to decode.</param>
		/// <param name="byteCount">The number of bytes to decode.</param>
		/// <param name="chars">The character array to contain the resulting set of characters.</param>
		/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
		/// <returns>The actual number of characters written into chars.</returns>
		public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
		{
			return _decoder.GetChars(bytes, byteIndex, byteCount, chars, charIndex);
		}

		/// <summary>
		/// Calculates the maximum number of bytes produced by encoding the specified number of characters.
		/// </summary>
		/// <param name="charCount">The number of characters to encode.</param>
		/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
		public override int GetMaxByteCount(int charCount)
		{
			return charCount;
		}

		/// <summary>
		/// Calculates the maximum number of characters produced by decoding the specified number of bytes.
		/// </summary>
		/// <param name="byteCount">The number of bytes to decode.</param>
		/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
		public override int GetMaxCharCount(int byteCount)
		{
			return byteCount;
		}

		/// <summary>
		/// Gets a value indicating whether the current encoding uses single-byte code points.
		/// </summary>
		public override bool IsSingleByte
		{
			get
			{
				return true;
			}
		}

		/// <summary>
		/// Gets the human-readable description of the current encoding.
		/// </summary>
		public override string EncodingName
		{
			get
			{
				return "Ukrainian (DOS)";
			}
		}
	}
}